<?php
class multicontainer extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('modmulticontainer');
		$this->load->model('modmulticontent');
	}
	
	function index($id_multi_content)
	{
		$page = 100;
		$token = $this->session->userdata('token');
		if($token){
			$data['idMultiContent'] = $id_multi_content;
			$data['results'] = $this->modmulticontainer->getList($id_multi_content,$page);
			$this->load->view('multicontainer/list',$data);
		}else{
			redirect('login');
		}
	}
    
	function add($id_multi_content){
		$data['mode'] = "Tambah Content";
		$data['action'] = "save";
		$data['idMultiContent'] = $id_multi_content;
		$data['menucontent'] = $this->modmulticontent->getComboContent();
		$this->load->view('multicontainer/form',$data);
	}
	
	function edit($id_multi_container,$id_multi_content){
		$data['mode'] = "Ubah Content";
		$data['action'] = "update";
		$data['idMultiContent'] = $id_multi_content;
		$data['detail'] = $this->modmulticontainer->detail($id_multi_container);
		$data['menucontent'] = $this->modmulticontent->getComboContent();
		$this->load->view('multicontainer/form',$data);
	}
	
	function save()
	{
		$id_multi_container = $this->input->post('id_multi_container');
		$id_multi_content = $this->input->post('id_multi_content');
		$title_multi_container = $this->input->post('title_multi_container');
		$date_multi_container = date('Y-m-d');
		$container_multi_content = $this->input->post('container_multi_content');
		
		$config['upload_path'] = './images/uploads/content_files';
		$config['allowed_types'] = 'pdf|docx|doc|png|jpeg|jpg|gif';
		
		$this->load->library('upload', $config);
		$this->upload->do_upload('file_upload');
		$getFile = $this->upload->data();
		$path_foto = $getFile['file_name'];
		
		if($path_foto != ""){	
			if ($this->upload->display_errors())
			{
				$this->session->set_flashdata('error', true);
				$file_upload="";
			}
			else{
				$file_upload = $getFile['file_name'];
			}
		}else{
			$file_upload = $this->input->post('file_upload_tmp');
		}
		
		$submit = $this->input->post('submit');	
		if($submit)
		{
			$this->modmulticontainer->setData($id_multi_container,$id_multi_content,$title_multi_container,$date_multi_container,$container_multi_content,$file_upload);
			$this->modmulticontainer->create();
			$this->session->set_flashdata('success', true);
			redirect('multicontainer/index/'.$id_multi_content);
		}
		$this->session->set_flashdata('error', true);
		redirect('multicontainer/index/'.$id_multi_content);
	}
	
	function update()
	{
		$id_multi_container = $this->input->post('id_multi_container');
		$id_multi_content = $this->input->post('id_multi_content');
		$title_multi_container = $this->input->post('title_multi_container');
		$date_multi_container = $this->input->post('date_multi_container');
		$container_multi_content = $this->input->post('container_multi_content');
		
		$config['upload_path'] = './images/uploads/content_files';
		$config['allowed_types'] = 'pdf|docx|doc|png|jpeg|jpg|gif';
		
		$this->load->library('upload', $config);
		$this->upload->do_upload('file_upload');
		$getFile = $this->upload->data();
		$path_foto = $getFile['file_name'];
		
		if($path_foto != ""){	
			if ($this->upload->display_errors())
			{
				$this->session->set_flashdata('error', true);
				$file_upload="";
			}
			else{
				$file_upload = $getFile['file_name'];
			}
		}else{
			$file_upload = $this->input->post('file_upload_tmp');
		}
		
		$submit = $this->input->post('submit');	
		if ($submit)
		{
			$this->modmulticontainer->setData($id_multi_container,$id_multi_content,$title_multi_container,$date_multi_container,$container_multi_content,$file_upload);
			$this->modmulticontainer->update($id_multi_container);
			$this->session->set_flashdata('success', true);
			redirect('multicontainer/index/'.$id_multi_content);
		}
		$this->session->set_flashdata('error', true);
		redirect('multicontainer/index/'.$id_multi_content);
	}
	
	function delete($id_multi_container,$id_multi_content)
	{
		$getHapus = $this->modmulticontainer->detail($id_multi_container);
		@unlink("images/uploads/content_files/".$getHapus[0]['file_upload']);
		
		if ($this->modmulticontainer->remove($id_multi_container)){
			$this->session->set_flashdata('delete', true);
			redirect('multicontainer/index/'.$id_multi_content,'refresh');
		}
		$this->session->set_flashdata('error', true);
		redirect('multicontainer/index/'.$id_multi_content);
	}	
	
	function searchData(){
		$name = $this->input->post('name');
		$data['results'] = $this->modmulticontainer->getListSearch($name);
		$this->load->view('multicontainer/list',$data);
	}
}
?>